package cn.argentoaskia.crm.controller;


import cn.argentoaskia.crm.bean.EmployeeWithDeptName;
import cn.argentoaskia.crm.result.ResponseData;
import cn.argentoaskia.crm.service.delete.EmployeeDeleteService;
import cn.argentoaskia.crm.service.page.EmployeeService;
import cn.argentoaskia.crm.service.update.EmployeeUpdateService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.converter.Converter;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

@RestController
@CrossOrigin
public class EmployeeController<T> {

    // springmvc里面怎么接收参数
    // URL：一般情况有两种：
    //  普通的URL
    //  http://localhost:8080/employees?username=zhangssan&password=123456
    //  RESTFul URl:
    //  GET http://localhost:8080/employees/zhangssan/123456
    //  占位符：{占位符名}，作用：绑定方法参数，靠@PathVariable，
    //  默认情况下，占位符内的占位符名和参数名相同，可以什么都不写，@PathVariable
    //  一般RESTful API 都靠各种@XXXMapping，比如@GetMapping、@PostMapping
//    @GetMapping("/employees/{user}/{password}")
//    public ResponseData employee(@PathVariable("user") String username, String password){
//
//    }

    // http://localhost:8080/employees?username=zhangssan&password=123456
    // http://localhost:8080/employees?page=2&limit=10
    // http://localhost:8080/employees?date=2022-05-10T15:03:31&limit=10
    // 普通URL参数接收方法：直接在方法中写参数即可,springmv默认会把username和password根据参数名来赋值参数。
    // 因此参数名必须相同
    // 类型写什么？  Converter<S, T>


    // 分页查询的控制器
    @Autowired
    private EmployeeService employeeService;
    @GetMapping("/employees")
    public ResponseData employee(int page, int limit){
        // 1.新建一个list
        List<EmployeeWithDeptName> employeeWithDeptNameList = new LinkedList<>();
        // 2.执行业务层的分页查询
        long count = employeeService.selectByPage(page, limit, employeeWithDeptNameList);

        // 3. 定义返回数据
        ResponseData responseData = new ResponseData();
        // 返回的数据总量
        responseData.setCount((int) count);
        // 返回的核心数据
        responseData.setData(employeeWithDeptNameList);
        // 返回的状态码
        responseData.setCode(0);
        // 返回的状态信息
        responseData.setInfo("success");
        return responseData;
    }

    @Autowired
    private EmployeeDeleteService employeeDeleteService;
    @DeleteMapping("/employees")
    public ResponseData deleteIds(@RequestBody String json) {
        boolean b = employeeDeleteService.deleteIds(json);
        ResponseData responseData = new ResponseData();
        if (b) {
            responseData.setCode(200);
            responseData.setInfo("success");
        } else {
            responseData.setCode(502);
            responseData.setInfo("fail");
        }
        return responseData;
    }

    @Autowired
    private EmployeeUpdateService employeeUpdateService;
    @PatchMapping("/employees")
    public ResponseData update(@RequestBody String json){
        JSONObject jsonObject = JSONObject.parseObject(json);
        Integer id = jsonObject.getInteger("id");
        String field = jsonObject.getString("field");
        String value = jsonObject.getString("value");
        boolean update = employeeUpdateService.update(id, field, value);
        ResponseData responseData = new ResponseData();
        if (update) {
            responseData.setCode(200);
            responseData.setInfo("success");
        } else {
            responseData.setCode(502);
            responseData.setInfo("fail");
        }
        return responseData;
    }
}
